package com.wisevar.thirdplatform.gateway.remote.sysmgr;

import com.alibaba.fastjson.JSON;
import com.aygframework.support.exception.AygBaseBizException;
import com.wisevar.thirdplatform.gateway.remote.ResponseCode;
import com.wisevar.thirdplatform.gateway.remote.sysmgr.model.ThirdPlatformDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Component
@Slf4j
public class SysMgrRemoteService {

    private static final String BASE_URL = "http://sysmgr-web";

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 获取第三方平台
     *
     * @return
     */
    public List<ThirdPlatformDTO> selectThirdPlatforms() {
        String url = BASE_URL + "/third-platform/list-all";
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
        if (response.getStatusCodeValue() != 200) {
            throw new AygBaseBizException(ResponseCode.SYS_ERR.getValue(), "请求" + url + "异常");
        }
        if (response.getBody() != null) {
            return JSON.parseArray(response.getBody(), ThirdPlatformDTO.class);
        }
        return null;
    }

}
